//#######################################################################################################
//##################################### Plugin-254 AlarmSensor ##########################################
//#######################################################################################################

/*********************************************************************************************\
 * This protocol provides support for some chinese Alarm "gadgets"
 * 
 * Author             : Martinus van den Broek
 * Support            : geen
 * Date               : 28 Okt 2013
 * Versie             : 0.1
 * Compatibility      : R744
 * Syntax             : "AlarmSensor <sensor ID>"
 *********************************************************************************************
 * Technical data:
 * Devices send 50 pulses. Manchester encoded, 25 bits
 * No further data, might be just a 25 bits ID
 \*********************************************************************************************/

#define PLUGIN_ID 254
#define PLUGIN_NAME "AlarmSensor"

boolean Plugin_254(byte function, struct NodoEventStruct *event, char *string)
{
  boolean success=false;

  switch(function)
  {
#ifdef PLUGIN_254_CORE
  case PLUGIN_RAWSIGNAL_IN:
    {
      RawSignal.Multiply=50;
      if (RawSignal.Number != 50) return false;

      unsigned long bitstream=0L;
      for(byte x=2;x<=48;x=x+2)
      {
        if ((RawSignal.Pulses[x-1]*RawSignal.Multiply < 600) && (RawSignal.Pulses[x]*RawSignal.Multiply < 600)) return false; // invalid manchestercode
        if ((RawSignal.Pulses[x-1]*RawSignal.Multiply > 600) && (RawSignal.Pulses[x]*RawSignal.Multiply > 600)) return false; // invalid manchestercode
        if (RawSignal.Pulses[x]*RawSignal.Multiply > 600) bitstream = (bitstream << 1) | 0x1; 
        else bitstream = bitstream << 1;
      }

      if (bitstream == 0) return false;

      event->Par1=0;
      event->Par2=bitstream;
      event->SourceUnit    = 0;
      event->Port          = VALUE_SOURCE_RF;
      event->Type          = NODO_TYPE_PLUGIN_EVENT;
      event->Command       = PLUGIN_ID;

      return true;
      break;
    }
  case PLUGIN_COMMAND:
    {
      break;
    }
#endif // PLUGIN_254_CORE

#if NODO_MEGA
  case PLUGIN_MMI_IN:
    {
    char* str=(char*)malloc(40);
    string[25]=0;
  
    if(GetArgv(string,str,1))
      {
      if(strcasecmp(str,PLUGIN_NAME)==0)
        {
        if(GetArgv(string,str,2))
          {
          event->Par1=str2int(str);    
          if(GetArgv(string,str,3))
            {
              event->Par2=str2int(str);
              event->Type = NODO_TYPE_PLUGIN_EVENT;
              event->Command = PLUGIN_ID;  
              success=true;
            }
          }
        }
      }
    free(str);
    break;
    }

  case PLUGIN_MMI_OUT:
    {
    strcpy(string,PLUGIN_NAME);
    strcat(string," ");
    strcat(string,int2str(event->Par1));
    strcat(string,",");
    strcat(string,int2str(event->Par2));
    break;
    }
#endif //NODO_MEGA
  }      
  return success;
}
